import { createStore } from 'vuex';
import { getToken } from '@/utils/storage.js';

export default createStore({
  state: {
    token: getToken(),
    user: null,
  },
  mutations: {
    SET_TOKEN(state, token) {
      localStorage.setItem('xmall-token', token);
      state.token = token;
    },
    SET_USER(state, user) {
      state.user = user;
    },
  },
  actions: {
    setToken({ commit }, token) {
      commit('SET_TOKEN', token);
    },
    setUser({ commit }, user) {
      commit('SET_USER', user);
    },
  },
  getters: {
    isAuthenticated: (state) => !!state.token,
  },
});